package com.imooc.springboot.study.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.imooc.springboot.study.config.SpringBootConfig;
import com.imooc.springboot.study.vo.Imoocer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <h1> 测试controller</h1>
 */
@Slf4j
@RequestMapping("springboot")
@RestController
public class MyController {

    @Value("${mydata.version}")
    private String version;
    @Value("${mydata.name}")
    private String name;

    @RequestMapping("data")
    public String tes() {

        log.info("版本号：{}    应用名称:{}", version, name);
        return "版本号：" + version + "应用名称" + name;
    }



    @Autowired
    private SpringBootConfig springBootConfig;

    @RequestMapping("data2")
    public String tes2() {

        log.info("版本号2：{}    应用名称2:{}", springBootConfig.getVersion(), springBootConfig.getName());
        return "版本号2：" + springBootConfig.getVersion() + "应用名称2" + springBootConfig.getName();
    }

    @Autowired
    private ObjectMapper mapper;
    @GetMapping("/jackson")
    public Imoocer jackson()throws Exception{
         Imoocer imoocer=Imoocer.builder()
                 .age(20)
                 .name("qinyi")
                 .address("shanghai")
                 .registerTime(new Date())
                 .build();
         log.info("imoocer: {}",imoocer.toString());
        String jsonImoocer = mapper.writeValueAsString(imoocer);
        log.info("将object转化为json:{} "+jsonImoocer);
        log.info("将json转化为object {}",mapper.readValue(jsonImoocer,Imoocer.class));
        return mapper.readValue(jsonImoocer,Imoocer.class);
    }
}
